home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
dte5_1.zip
/
UTILS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-02-06
|
56KB
|
1,884 lines
/*
* Written by Douglas Thomson (1989/1990)
*
* This source code is released into the public domain.
*/
/*
* Name: dte - Doug's Text Editor program - miscellaneous utilities
* Purpose: This file contains miscellaneous functions that were required
* in more than one of the other files, or were thought to be
* likely to be used elsewhere in the future.
* File: utils.c
* Author: Douglas Thomson
* System: this file is intended to be system-independent
* Date: October 1, 1989
*/
#ifdef HPXL
#include "commonh"
#include "utilsh"
#else
#include "common.h"
#include "utils.h"
#endif
#include <time.h>
#ifdef __TURBOC__
#include <dir.h> /* for making temporary file names etc */
#endif
/*
* prototypes for all functions in this file
*/
int myisalnum ARGS((char c));
int linelen ARGS((text_ptr s));
int prelinelen ARGS((text_ptr s));
text_ptr find_next ARGS((text_ptr s));
text_ptr find_prev ARGS((text_ptr current));
void copy_line ARGS((windows *window));
void un_copy_line ARGS((windows *window));
int expand ARGS((text_ptr dest, text_ptr end));
int load_file ARGS((char *name, int fixup));
void set_prompt ARGS((char *prompt, int lines));
int get_name ARGS((char *prompt, int lines, char *name));
void fix_marks ARGS((windows *window, text_ptr pos, long len));
int get_ynaq ARGS((windows *window));
int get_yn ARGS((windows *window));
int get_oa ARGS((windows *window));
char get_attr ARGS((windows *window, text_ptr text));
int update_line ARGS((windows *window, text_ptr orig, int line,
text_ptr cursor));
int display_window ARGS((windows *window, int last, text_ptr cursor, int wn));
int display ARGS((do_func doit, int reserved));
void setup_window ARGS((windows *window));
int first_non_blank ARGS((char *s));
void page_up ARGS((windows *window));
void page_down ARGS((windows *window));
void scroll_down ARGS((windows *window));
void scroll_up ARGS((windows *window));
void save_file ARGS((windows *window, int kind));
void save_as_file ARGS((windows *window));
/*
* Name: myisalnum
* Purpose: To determine whether or not a character is part of a "word",
* which in languages like Pascal means a letter, digit or
* underscore.
* Date: October 1, 1989
* Passed: c: the character to be tested
* Returns: TRUE if c is an alphanumeric or '_' character, FALSE otherwise
*/
int myisalnum(c)
char c;
{
if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
(c == '_')) {
return TRUE;
}
return FALSE;
}
/*
* Name: linelen
* Purpose: To determine the length of a line, up to either a \n or a
* \0, whichever comes first.
* Date: October 1, 1989
* Passed: s: the line to be measured
* Returns: the length of the line
*/
int linelen(s)
text_ptr s;
{
int len = 0;
while (*s && *s != '\n') {
++len;
++s;
}
return len;
}
/*
* Name: prelinelen
* Purpose: To determine the length of a line, from the current position
* backwards to either a \n or a \0, whichever comes first.
* Date: October 1, 1989
* Passed: s: the line to be measured
* Returns: the length of the line up to the current position
* Notes: It is assumed there will be a "terminating" \0 before the
* start of the first line. This is the case with the main
* text buffer, but elsewhere beware.
*/
int prelinelen(s)
text_ptr s;
{
int len = 0;
while (*--s && *s != '\n') {
++len;
}
return len;
}
/*
* Name: find_next
* Purpose: To find the first character in the next line after the starting
* point.
* Date: October 1, 1989
* Passed: s: the starting point
* Returns: the first character in the next line
*/
text_ptr find_next(s)
text_ptr s;
{
while (*s && *s != '\n') {
++s;
}
if (*s) {
return ++s;
}
return NULL;
}
/*
* Name: find_prev
* Purpose: To find the start of the line before the current line.
* Date: October 1, 1989
* Passed: current: the current line
* Returns: the start if the previous line
* Notes: current must be at the start of the current line to begin with.
* There must be a \0 preceding the first line.
*/
text_ptr find_prev(current)
text_ptr current;
{
if (*--current == '\0') {
return NULL;
}
for (;;) {
if (*--current == '\n' || *current == '\0') {
return ++current;
}
}
}
/*
* Name: copy_line
* Purpose: To copy the cursor line, if necessary, into the current line
* buffer, so that changes can be made efficiently.
* Date: October 1, 1989
* Passed: window: access to the current line
* Notes: As the cursor line is being copied, any markers that are set
* within the line are also copied.
* Trailing spaces left on the line (presumably from earlier
* editing) are removed during the copy.
* See un_copy_line, the reverse operation.
*/
void copy_line(window)
windows *window;
{
text_ptr p, q; /* destination and source of copy */
int count; /* number of characters copied */
int i; /* for updating markers */
text_ptr end_line; /* end of line after removing trailing spaces */
/*
* If the line has already been copied, then do not copy it again
*/
if (g_status.copied) {
return;
}
/*
* record that the current line buffer is active
*/
g_status.copied = TRUE;
/*
* clear any old buffer markers left from last time
*/
for (i=0; i < NO_MARKS; i++) {
g_status.buff_marker[i] = NULL;
}
/*
* find out where the line should end after removing trailing
* spaces.
*/
end_line = q = window->cursor;
while (*q && *q != '\n') {
if (*q++ != ' ') {
end_line = q;
}
}
/*
* copy the cursor line to the line buffer, noting any markers
* passed along the way
*/
p = g_status.line_buff;
q = window->cursor;
for (count=0; ; ) {
for (i=0; i < NO_MARKS; i++) {
if (q == window->file_info->marker[i]) {
g_status.buff_marker[i] = p;
}
}
if (*q == '\n') {
*p++ = *q++;
break;
}
if (*q == '\0') {
break;
}
/*
* avoid copying trailing spaces
*/
if (q < end_line) {
if (++count >= BUFF_SIZE) {
error(WARNING, "line buffer overflow - line truncated!");
break;
}
*p++ = *q;
}
++q;
}
*p = '\0';
}
/*
* Name: un_copy_line
* Purpose: To copy the cursor line, if necessary, from the current line
* buffer, shifting the main text to make the right amount of
* room.
* Date: October 1, 1989
* Passed: window: access to the current line
* Notes: As the cursor line is being copied, any markers that are set
* within the line buffer are also copied.
* For various reasons, trailing spaces are NOT removed when
* returning the line buffer to the main text. Typically,
* padding is added at the end of a line by deliberately
* adding trailing spaces, and then uncopying the line.
* See copy_line, the reverse operation.
*/
void un_copy_line(window)
windows *window;
{
text_ptr source; /* source for block move and for copying buffer line */
text_ptr dest; /* destination for block move and copy */
long number; /* length of block move */
int len; /* length of current line buffer text */
int curs_len; /* length of cursor line */
int i; /* used for checking markers */
/*
* do not uncopy unless the line buffer is active